
package com.lxj.database;

import com.lxj.utils.L;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class SQLiteDB {
	private String TAG = "InfoStorage";

	private APP_SQLiteOpenHelper mSQLiteOpenHelper = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	static SQLiteDB mInstance = null;
	Context mContext = null;

	private SQLiteDB(Context aContext) {
		mSQLiteOpenHelper = new APP_SQLiteOpenHelper(aContext);
		mSQLiteDatabase = mSQLiteOpenHelper.getWritableDatabase();
		mContext = aContext;
	}

	public SQLiteDatabase getSQLiteDatabase() {
		return mSQLiteDatabase;
	}

	public Context getContext() {
		return mContext;
	}

	public static SQLiteDB getInstance(Context aContext) {
		if (mInstance == null) {
			mInstance = new SQLiteDB(aContext);
		}
		return mInstance;
	}

	public void ClearTable(String aTableName) {
		try {
			if (mSQLiteDatabase != null) {
				mSQLiteDatabase.execSQL("DROP TABLE IF EXISTS " + aTableName);
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
		}
	}

	public boolean CreateTable(String aSql) {
		boolean ret = false;
		try {
			if (aSql != null && aSql.length() > 0) {
				if (mSQLiteDatabase != null) {
					try {
						mSQLiteDatabase.execSQL(aSql);
						ret = true;
					} catch (Exception e) {
						ret = false;
						L.e(TAG, "createDB failed: " + e.getMessage());
					}
				}
			} else {
				ret = false;
				L.e(TAG, "createDB failed: sql == null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
		}
		return ret;
	}

	public Cursor query(String aTable, String aSelection, String[] aColumns,
			String aOrderBy) {
		Cursor ret = null;
		try {
			if (mSQLiteDatabase != null) {
				ret = mSQLiteDatabase.query(aTable, aColumns, aSelection, null,
						null, null, aOrderBy);
			} else {
				L.e(TAG, "query failed:  [" + aTable
						+ "] mSQLiteDatabase == null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
			ret = query(aTable, aSelection, aColumns, aOrderBy);
		}
		return ret;
	}

	private void notableException(Exception e) {
		String exceptionstr = e.toString();
		if (exceptionstr != null && exceptionstr.contains("no such table")) {
			APP_SQLiteOpenHelper.createData(mSQLiteDatabase);
		}
	}

	public Cursor execSql(String aSql, String[] selectionArgs) {
		Cursor ret = null;
		try {

			if (mSQLiteDatabase != null) {
				ret = mSQLiteDatabase.rawQuery(aSql, selectionArgs);
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
			ret = execSql(aSql, selectionArgs);
		}
		return ret;
	}

	// public final Cursor query(String aTable, String[] aSelectionArgs)
	// {
	// if (mSQLiteDatabase != null)
	// {
	// return mSQLiteDatabase.rawQuery(aTable, aSelectionArgs);
	// }
	// else
	// {
	// EngineLog.e(TAG, "query failed: [" + aTable +
	// "] mSQLiteDatabase == null");
	// return null;
	// }
	// }

	public int delete(String aTable, String aWhereClause, String[] aWhereArgs) {
		try {
			if (mSQLiteDatabase != null) {
				return mSQLiteDatabase.delete(aTable, aWhereClause, aWhereArgs);
			} else {
				L.e(TAG, "delete failed: [" + aTable
						+ "] mSQLiteDatabase == null");
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
			return delete(aTable, aWhereClause, aWhereArgs);
		}
	}

	public long insert(String aTable, String aNullColumnHack,
			ContentValues aValues) {
		try {
			if (mSQLiteDatabase != null) {
				return mSQLiteDatabase.insert(aTable, aNullColumnHack, aValues);
			} else {
				L.e(TAG, "insert failed: [" + aTable + "] mSQLiteDatabase == null");
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
			return insert(aTable, aNullColumnHack, aValues);
		}
	}

	public void beginTransaction() {
		if (mSQLiteDatabase != null) {
			mSQLiteDatabase.beginTransaction();
		}
	}

	public void endTransaction() {
		if (mSQLiteDatabase != null) {
			mSQLiteDatabase.endTransaction();
		}
	}
	
	public boolean inTransaction()
	{
		if (mSQLiteDatabase != null) {
			return mSQLiteDatabase.inTransaction();
		}
		else
		{
			return false;
		}
	}

	public void commitTransaction() {
		if (mSQLiteDatabase != null && mSQLiteDatabase.inTransaction()) {
			mSQLiteDatabase.setTransactionSuccessful();
		}
	}

	public int update(String aTable, ContentValues aValues,
			String aWhereClause, String[] aWhereArgs) {
		int ret = -1;
		try {
			if (mSQLiteDatabase != null) {
				ret = mSQLiteDatabase.update(aTable, aValues, aWhereClause,
						aWhereArgs);
			} else {
				L.e(TAG, "update failed: [" + aTable
						+ "] mSQLiteDatabase == null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			notableException(e);
			ret = update(aTable, aValues, aWhereClause, aWhereArgs);
		}

		return ret;
	}

	@Override
	protected final void finalize() {
		if (mSQLiteDatabase != null) {
			mSQLiteDatabase.close();
			mSQLiteDatabase = null;
		}
	}
}
